package com.jx.lcs.versionInfo.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jx.lcs.util.common.CharUtil;
import com.jx.lcs.versionInfo.entity.VersionInfo;
import com.jx.lcs.versionInfo.service.VersionInfoService;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.persistence.PropertyFilter;
import com.tianyu.jty.common.web.BaseController;

/**
 * @author 陈新雷 2015年8月4日下午2:40:14
 *
 */
@Controller
@RequestMapping("verinfo")
public class VersionInfoController extends BaseController {
	private static final Logger logger = LoggerFactory
			.getLogger(VersionInfoController.class);
	
	@Autowired
	private VersionInfoService vis;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "versionInfo/versionInfoList";
	}

	/**
	 * 得到全部App版本数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getAll", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAll(HttpServletRequest request) {
		try{
			Page<VersionInfo> page = getPage(request);
			List<PropertyFilter> filters = PropertyFilter
					.buildFromHttpRequest(request);
			page = vis.search(page, filters);
			return getEasyUIData(page);
		} catch(Exception e) {
			logger.error("--得到全部App版本数据出现异常！", e);
			
			return null;
		}
		
	}

	/**
	 * 添加版本跳转
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("vi", new VersionInfo());
		model.addAttribute("action", "create");
		return "versionInfo/versionInfoForm";
	}

	/**
	 * 增加版本
	 * 
	 * @param KnowledgeInfo
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(@Valid VersionInfo vi, Model model) {
		if ("".equals(vi.getId()))
			vi.setId(null);
		
		vi.setAppurl(CharUtil.charConvert(vi.getAppurl()));
		
		vis.save(vi);
		return "success";
	}

	/**
	 * 修改版本跳转
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") String id, Model model) {
		model.addAttribute("vi", vis.get(id));
		model.addAttribute("action", "update");
		return "versionInfo/versionInfoForm";
	}

	/**
	 * 修改新闻
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute("vi") VersionInfo vi,
			Model model) {
		VersionInfo tem = vis.get(vi.getId());
		tem.setOs(vi.getOs());
		tem.setAppname(vi.getAppname());
		tem.setUpdatestatus(vi.getUpdatestatus());
		tem.setAppurl(CharUtil.charConvert(vi.getAppurl()));
		tem.setVersion(vi.getVersion());
		vis.update(tem);

		return "success";
	}
	/**
	 * 根据id删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") String id) {
		vis.delete(id);
		return "success";
	}
}
